package com.abbas.wallerpaper.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.abbas.wallerpaper.base.BaseActivity;
import com.abbas.wallerpaper.entity.ImageBean;
import com.abbas.wallerpaper.fragment.ContainFragment;
import com.abbas.wallerpaper.R;
import com.bumptech.glide.Glide;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DetailActivity extends BaseActivity implements View.OnClickListener{

    private ImageBean.ResBean.VerticalBean imgBean;
    private ImageView image;
    private Button set;
    private AlertDialog dialog;
    private TextView system, lock, both;

    @Override
    protected int getLayoutId() {
//        makeStatusBarTransparent(this);
        return R.layout.activity_detail;
    }

    @Override
    protected void init() {
        imgBean = (ImageBean.ResBean.VerticalBean) getIntent().getSerializableExtra("data");

        image = (ImageView) findViewById(R.id.image);
        set = (Button) findViewById(R.id.set);

        Glide.with(this)
                .load(imgBean.getImg())
                .into(image);
    }

    @Override
    protected void initEvent() {
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View container = LayoutInflater.from(DetailActivity.this).inflate(R.layout.settting_wapper_dialog, null, false);
                system = container.findViewById(R.id.sys);
                lock = container.findViewById(R.id.lock);
                both = container.findViewById(R.id.both);
                system.setOnClickListener(DetailActivity.this);
                lock.setOnClickListener(DetailActivity.this);
                both.setOnClickListener(DetailActivity.this);
                dialog = new AlertDialog.Builder(DetailActivity.this)
                        .setView(container)
                        .create();
                dialog.show();
            }
        });
    }

    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 通过设置全屏，设置状态栏透明
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明，否则导航栏会呈现系统默认的浅灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                attributes.flags |= flagTranslucentStatus;
                window.setAttributes(attributes);
            }
        }
    }


//    private void setSystemWallpaper() {
//
//    }

//    private void SetLockWallPaper(int resId) {
//        // TODO Auto-generated method stub
//        try {
//            WallpaperManager mWallManager = WallpaperManager.getInstance(this);
//            Class class1 = mWallManager.getClass();//获取类名
//            Method setWallPaperMethod = class1.getMethod("setBitmapToLockWallpaper", Bitmap.class);//获取设置锁屏壁纸的函数
//            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
//            setWallPaperMethod.invoke(mWallManager, BitmapFactory.decodeFile(drawableToStrPath(getApplicationContext(),resId,"myWallper")));//调用锁屏壁纸的函数，并指定壁纸的路径imageFilesPath
////            Toast.makeText(ImageViewActivity.this, "锁屏壁纸设置成功", Toast.LENGTH_SHORT).show();
//        } catch (Throwable e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }

    /**
     * drawable转为file
     *
     * @param mContext
     * @param drawableId drawable的ID
     * @param fileName   转换后的文件名
     * @return
     */
    public File drawableToFile(Context mContext, int drawableId, String fileName) {
//        InputStream is = view.getContext().getResources().openRawResource(R.drawable.logo);
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId);
//        Bitmap bitmap = BitmapFactory.decodeStream(is);

        String defaultPath = mContext.getFilesDir()
                .getAbsolutePath() + "/defaultGoodInfo";
        File file = new File(defaultPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String defaultImgPath = defaultPath + "/" + fileName;
        file = new File(defaultImgPath);
        try {
            file.createNewFile();

            FileOutputStream fOut = new FileOutputStream(file);

            bitmap.compress(Bitmap.CompressFormat.PNG, 20, fOut);
//            is.close();
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    public String drawableToStrPath(@NonNull Context mContext, int drawableId, String fileName) {
//        InputStream is = view.getContext().getResources().openRawResource(R.drawable.logo);
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId);
//        Bitmap bitmap = BitmapFactory.decodeStream(is);

        String defaultPath = mContext.getFilesDir()
                .getAbsolutePath() + "/defaultGoodInfo";
        File file = new File(defaultPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return defaultPath + "/" + fileName;
    }

    @Override
    public void onClick(View view) {
        WallpaperManager wpm = (WallpaperManager) getSystemService(
                Context.WALLPAPER_SERVICE);
        switch (view.getId()){
            case R.id.sys:
                try {
                    wpm.setBitmap(((BitmapDrawable) image.getDrawable()).getBitmap(), null, true, WallpaperManager.FLAG_SYSTEM);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.lock:
                try {
                    wpm.setBitmap(((BitmapDrawable) image.getDrawable()).getBitmap(), null, true, WallpaperManager.FLAG_LOCK);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.both:
                try {
                    wpm.setBitmap(((BitmapDrawable) image.getDrawable()).getBitmap());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
        dialog.dismiss();
    }
}